package com.lsj.gulimall.product.service.impl;

import com.lsj.gulimall.product.dao.CategoryBrandRelationDao;
import com.lsj.gulimall.product.entity.CategoryBrandRelationEntity;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsj.common.utils.PageUtils;
import com.lsj.common.utils.Query;

import com.lsj.gulimall.product.dao.BrandDao;
import com.lsj.gulimall.product.entity.BrandEntity;
import com.lsj.gulimall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Resource
    CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params), queryWrapper);

        return new PageUtils(page);
    }

    /**
     * 同时更新brand表和品牌关系表
     * @param brand
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDetail(BrandEntity brand) {
        this.baseMapper.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())) {
            CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
            categoryBrandRelationEntity.setBrandName(brand.getName());
            QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("brand_id",brand.getBrandId());
            categoryBrandRelationDao.update(categoryBrandRelationEntity,wrapper);
        }
    }

    @Override
    public List<BrandEntity> getCategroyRelationBrands(Long catId) {
        List<CategoryBrandRelationEntity> relations = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandEntity> brands = null;
        if (relations != null) {
            brands = relations.stream().map(relation -> {
                BrandEntity brand = this.baseMapper.selectById(relation.getBrandId());
                return brand;
            }).collect(Collectors.toList());
        }
        return brands;
    }

    /**
     * 删除品牌和品牌关联关系
     * @param brandIds
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBrandAndBrandRelation(List<Long> brandIds) {

        if (brandIds != null && brandIds.size() > 0) {
            this.baseMapper.deleteBatchIds(brandIds);
            List<CategoryBrandRelationEntity> relations = categoryBrandRelationDao.selectByBrandIds(brandIds);
            if (relations != null && relations.size() > 0) {
                List<Long> relationIds = relations.stream().map(relation -> {
                    return relation.getId();
                }).collect(Collectors.toList());
                categoryBrandRelationDao.deleteBatchIds(relationIds);
            }
        }

    }

}